package com.jing.android.util;

import java.awt.Dimension;
import java.awt.Toolkit;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;

import javax.swing.JEditorPane;
import javax.swing.JFrame;
import javax.swing.JScrollPane;


/**
 * Java-浏览器
 *
 * @author JingTuo
 */
public class Browser {

    private static final Logger LOGGER = Logger.getLogger(Browser.class.getSimpleName());

    private JFrame mContainer;

    private JScrollPane mScroller;

    private JEditorPane mUrlLoader;

    public Browser() {
        mContainer = new JFrame("浏览器");
        //点击关闭的时候退出
        mContainer.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        Dimension screenDimension = Toolkit.getDefaultToolkit().getScreenSize();

        int browserWidth = screenDimension.width;
        int browserHeight = screenDimension.height - 100;

        //用于加载网页
        mUrlLoader = new JEditorPane();
        mUrlLoader.setEditable(false);
        mContainer.add(mUrlLoader);

        mScroller = new JScrollPane(mUrlLoader);
        mScroller.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
        mScroller.setPreferredSize(new Dimension(browserWidth, browserHeight));
        mScroller.setMinimumSize(new Dimension(800, 800));
        //必须设置尺寸, 否则仅显示标题栏
        mContainer.setPreferredSize(new Dimension(browserWidth, browserHeight));
    }


    public void start() {
        mContainer.setVisible(true);
    }

    public void open(String url) {
        try {
            mUrlLoader.setPage(url);
        } catch (IOException e) {
            LOGGER.log(Level.WARNING, e.getMessage(), e);
        }
    }

    public void close() {

    }

    public static void main(String[] args) {
        Browser browser = new Browser();
        browser.start();
        browser.open("https://www.baidu.com/");
    }

}
